<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓位名称" prop="equipmentName">
        <el-input v-model="queryParams.storeLocationName" placeholder="请输入仓位名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓位编码" prop="equipmentCode">
        <el-input v-model="queryParams.storeLocationCode" placeholder="请输入仓位编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入监控设备名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入监控设备编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="推送时间" prop="warningPushTimeRange">
        <div class="block">
          <el-date-picker v-model="queryParams.warningPushTimeRange" type="datetimerange"
                          format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                          align="right">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['platform:push:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pushList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统编号" align="center" prop="warningPushId" />
      <el-table-column label="预警ID" align="center" prop="warningId" />
      <el-table-column label="监控设备名称"  width="100" align="center" prop="equipmentName" :show-overflow-tooltip="true"/>
      <el-table-column label="监控设备编码"  width="100" align="center" prop="equipmentCode" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位名称" align="center" prop="storeLocationName" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位编码" align="center" prop="storeLocationCode" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位预置点位"  width="100" align="center" prop="presetId" />
      <el-table-column label="预警类型" width="120" align="center" prop="warningType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform_warning_type" :value="scope.row.warningType" />
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="center" prop="warningPushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warningPushTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送状态" width="100" align="center" prop="warningPushStatus" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform_warningpush_status" :value="scope.row.warningPushStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['platform:push:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预警ID" prop="warningId">
          <el-input v-model="form.warningId" placeholder="请输入预警ID" />
        </el-form-item>
        <el-form-item label="预警推送内容">
          <editor v-model="form.warningPushContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="预警时间" prop="warningPushTime">
          <el-date-picker clearable
            v-model="form.warningPushTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预警推送结果" prop="warningPushResult">
          <el-input v-model="form.warningPushResult" placeholder="请输入预警推送结果" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--查看详情对话框 -->
    <el-dialog title="查看预警详情" :close="cancelDteail" :visible.sync="viewOpen" width="800px" append-to-body>
      <div>
        <el-form label-width="120px" v-if="viewOpen">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备名称：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.equipmentName }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备编码：" prop="equipmentChannel" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.equipmentCode }}</el-text>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="仓位名称：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.storeLocationCode }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="仓位编码："  :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.storeLocationName }}</el-text>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预置点位ID：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.presetId }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预警状态：" :show-overflow-tooltip="true">
                <dict-tag :options="dict.type.platform_warning_type"
                  :value="this.viewData.warningType" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预警时间：" :show-overflow-tooltip="true">
                {{ parseTime(this.viewData.warningTime, '{y}-{m}-{d} {h}:{i}:{s}')
                }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建时间：" :show-overflow-tooltip="true">
                {{ parseTime(this.viewData.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
                }}
              </el-form-item>
            </el-col>
          </el-row>
         <el-row>
            <el-col :span="12">
              <el-form-item label="推送时间：" :show-overflow-tooltip="true">
                {{ parseTime(this.viewData.warningTime, '{y}-{m}-{d} {h}:{i}:{s}')
                }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="推送状态：" :show-overflow-tooltip="true">
                <dict-tag :options="dict.type.platform_warningpush_status"
                  :value="this.viewData.warningPushStatus" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="推送内容：">
            <el-input v-model="this.viewData.warningPushContent" type="textarea" placeholder="推送内容" disabled />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" v-if="viewOpen">
        <el-button @click="cancelDteail" icon="el-icon-back">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import { listPush, getPush, delPush, addPush, updatePush } from "@/api/platform/push"
import moment from 'moment';
export default {
  name: "Push",
  dicts: ['platform_warningpush_status','platform_warning_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      viewOpen: false,
      viewData: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      pushList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warningId: null,
        warningPushContent: null,
        warningPushTime: null,
        warningPushStatus: null,
        warningPushResult: null,
        warningPushTimeRange:null,
        warningPushStartTime:null,
        warningPushEndTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warningId: [
          { required: true, message: "预警ID不能为空", trigger: "blur" }
        ],
        warningPushContent: [
          { required: true, message: "预警推送内容不能为空", trigger: "blur" }
        ],
        warningPushTime: [
          { required: true, message: "预警时间不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      if (Array.isArray(this.queryParams.warningPushTimeRange)) {
        this.queryParams.warningPushStartTime = moment(this.queryParams.warningPushTimeRange[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.warningPushEndTime = moment(this.queryParams.warningPushTimeRange[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listPush(this.queryParams).then(response => {
        this.pushList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    handleTimeChange(val) {
      // 时间选择变化时的处理
      console.log('选择的时间范围:', val)
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        warningPushId: null,
        warningId: null,
        warningPushContent: null,
        warningPushTime: null,
        warningPushStatus: null,
        warningPushResult: null,
        createBy: null,
        createTime: null
      }
      this.resetForm("form")
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warningPushId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【请填写功能名称】"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const warningPushId = row.warningPushId || this.ids
      getPush(warningPushId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改【请填写功能名称】"
      })
    },
        /** 查看按钮操作 */
    handleView(row) {
      this.viewOpen = true
      this.viewData=row
    },
    cancelDteail() {
       this.viewOpen = false
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warningPushId != null) {
            updatePush(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addPush(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warningPushIds = row.warningPushId || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + warningPushIds + '"的数据项？').then(function() {
        return delPush(warningPushIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('platform/push/export', {
        ...this.queryParams
      }, `push_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
